﻿using System;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.Windows.Interactivity;

namespace OpenLightGroup.Common.Behaviors
{
    /// <summary>
    /// Invokes the associated actions when the current user logs in.
    /// </summary>
    /// <remarks>
    /// Uses WebContextBase.Current.Authentication.LoggedIn event.
    /// </remarks>
	public class UserLoggedInTrigger : TriggerBase<DependencyObject>
	{
        /// <summary>
        /// Writes up the event handler to WebContextBase.Current.Authentication.LoggedIn
        /// </summary>
		protected override void OnAttached()
		{
			base.OnAttached();			
            WebContextBase.Current.Authentication.LoggedIn += new EventHandler<AuthenticationEventArgs>(Authentication_LoggedIn);
		}

        /// <summary>
        /// Removes the event handler to WebContextBase.Current.Authentication.LoggedIn
        /// </summary>
		protected override void OnDetaching()
		{
			base.OnDetaching();
            WebContextBase.Current.Authentication.LoggedIn -= Authentication_LoggedIn;
			
		}

       private void Authentication_LoggedIn(object sender, AuthenticationEventArgs e)
        {
           this.InvokeActions(sender);
        }

	}
}